package com.ruoyi.hikvision.controller;

import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hikvision.domain.HikvisionDevice;
import com.ruoyi.hikvision.service.IHikvisionDeviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备管理Controller
 * 
 * @author ruoyi
 * @date 2025-01-02
 */
@RestController
@RequestMapping("/hikvision/device")
public class HikvisionDeviceController extends BaseController
{
    @Autowired
    @Qualifier("redisSecondTemplate")
    public RedisTemplate redisSecondTemplate;

    @Autowired
    private IHikvisionDeviceService hikvisionDeviceService;

    /**
     * 查询设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('hikvision:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(HikvisionDevice hikvisionDevice)
    {
        startPage();
        List<HikvisionDevice> list = hikvisionDeviceService.selectHikvisionDeviceList(hikvisionDevice);
        return getDataTable(list);
    }

    /**
     * 查询设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('hikvision:device:lists')")
    @GetMapping("/lists")
    public AjaxResult lists(HikvisionDevice hikvisionDevice)
    {
        List<HikvisionDevice> list = hikvisionDeviceService.selectHikvisionDeviceList(hikvisionDevice);
        return success(list);
    }

    /**
     * 导出设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('hikvision:device:export')")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HikvisionDevice hikvisionDevice)
    {
        List<HikvisionDevice> list = hikvisionDeviceService.selectHikvisionDeviceList(hikvisionDevice);
        ExcelUtil<HikvisionDevice> util = new ExcelUtil<HikvisionDevice>(HikvisionDevice.class);
        util.exportExcel(response, list, "设备管理数据");
    }

    /**
     * 获取设备管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('hikvision:device:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(hikvisionDeviceService.selectHikvisionDeviceById(id));
    }

    /**
     * 新增设备管理
     */
    @PreAuthorize("@ss.hasPermi('hikvision:device:add')")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HikvisionDevice hikvisionDevice)
    {
        return toAjax(hikvisionDeviceService.insertHikvisionDevice(hikvisionDevice));
    }

    /**
     * 修改设备管理
     */
    @PreAuthorize("@ss.hasPermi('hikvision:device:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HikvisionDevice hikvisionDevice)
    {
        return toAjax(hikvisionDeviceService.updateHikvisionDevice(hikvisionDevice));
    }

    /**
     * 删除设备管理
     */
    @PreAuthorize("@ss.hasPermi('hikvision:device:remove')")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hikvisionDeviceService.deleteHikvisionDeviceByIds(ids));
    }

    /**
     * 登陆设备管理
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody HikvisionDevice hikvisionDevice)
    {
        System.out.println(hikvisionDevice);

        String key = "mrx-test";
        //Boolean success = redisSecondTemplate.opsForValue().setIfAbsent(key, "123");
        //redisSecondTemplate.opsForValue().set(key,"myValue666");

        //redisSecondTemplate.opsForValue().set("mrx", "123");
        redisSecondTemplate.opsForValue().set("mrx", "123", 60, TimeUnit.MINUTES);

        //HkManage.login();


        return toAjax(1);
    }
}
